package winOrDie.Net.Http.httpmsg;

import java.io.CharArrayReader;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;

public class WoD_XmlParsing_Client {
	/*
	 * Parseo de mensajes Xml -> String, String -> Xml
	 * */
   public static Document ParseStringToXmlDocument(String xml_String) throws SAXException, IOException, ParserConfigurationException
   {   
	   InputSource is = null;	   
	   //IMPORTANTE PARA PARSEAR STRINGS A DOCUMENT XML
	   is = new InputSource(new CharArrayReader(xml_String.toCharArray()));
	   
	   DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
	   DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	   Document doc = docBuilder.parse(is);
	   
	   return doc;
   }
   
   public static String ParseXmlDocumentToString(Document XmlDoc) throws TransformerException
   {
	   Transformer transformer = TransformerFactory.newInstance().newTransformer();
	   transformer.setOutputProperty(OutputKeys.INDENT, "no");

	   //initialize StreamResult with File object to save to file
	   StreamResult result = new StreamResult(new StringWriter());
	   DOMSource source = new DOMSource(XmlDoc);
	   transformer.transform(source, result);

	   String xmlString = result.getWriter().toString();
	   
	   return xmlString;		   
   }
   
   public static Document getLoginMsg(String LoginName, String Password)
   {
	   DocumentBuilderFactory dbFactory = DocumentBuilderFactoryImpl.newInstance();
	   DocumentBuilder docBuilder;
	   try {
		   docBuilder = dbFactory.newDocumentBuilder();

		   Document XmlResult = docBuilder.newDocument();

		   Element docEleResult = XmlResult.createElement(WoD_Constants.TAG_MESSAGE);

		   docEleResult.setAttribute(WoD_Constants.ATR_TYPE, WoD_Constants.MSG_TYPE_DOLOGIN_REQUEST);

		   Element Result = XmlResult.createElement(WoD_Constants.TAG_LOGINNAME);
		   Result.setAttribute(WoD_Constants.ATR_NAME, LoginName);
		   Result.setAttribute(WoD_Constants.ATR_PWD, Password);

		   docEleResult.appendChild(Result);		   
		   XmlResult.appendChild(docEleResult);

		   return XmlResult;
	   } catch (ParserConfigurationException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
	   }
	   return null;
	   
   }

   public static Document getNewGameMsg(WoD_InfoPartidasActuales wipa)
   {
	   DocumentBuilderFactory dbFactory = DocumentBuilderFactoryImpl.newInstance();
	   DocumentBuilder docBuilder;
	   try {
		   docBuilder = dbFactory.newDocumentBuilder();

		   Document XmlResult = docBuilder.newDocument();

		   Element docEleResult = XmlResult.createElement(WoD_Constants.TAG_MESSAGE);

		   docEleResult.setAttribute(WoD_Constants.ATR_TYPE, WoD_Constants.MSG_TYPE_NEWGAME_REQUEST);

		   Element Result = XmlResult.createElement(WoD_Constants.TAG_SERVERNAME);
		   Result.setAttribute(WoD_Constants.ATR_VALUE, wipa.getServerName());
		   docEleResult.appendChild(Result);		   
		   
		   Element Result2 = XmlResult.createElement(WoD_Constants.TAG_SERVERIP);
		   Result2.setAttribute(WoD_Constants.ATR_VALUE, wipa.getServerIP());
		   docEleResult.appendChild(Result2);
		   
		   Element Result3 = XmlResult.createElement(WoD_Constants.TAG_SERVERPORT);
		   Result3.setAttribute(WoD_Constants.ATR_VALUE, wipa.getServerPort());
		   docEleResult.appendChild(Result3);
		   
		   Element Result4 = XmlResult.createElement(WoD_Constants.TAG_SERVERSTATUS);
		   Result4.setAttribute(WoD_Constants.ATR_VALUE, wipa.getServerStatus());
		   docEleResult.appendChild(Result4);
		   
		   Element Result5 = XmlResult.createElement(WoD_Constants.TAG_GAMETITLE);
		   Result5.setAttribute(WoD_Constants.ATR_VALUE, wipa.getGameTitle());
		   docEleResult.appendChild(Result5);
		   
		   XmlResult.appendChild(docEleResult);
		   return XmlResult;
	   } catch (ParserConfigurationException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
	   }
	   return null;
   }

   public static Document getUpdateGameMsg(WoD_InfoPartidasActuales wipa)
   {
	   DocumentBuilderFactory dbFactory = DocumentBuilderFactoryImpl.newInstance();
	   DocumentBuilder docBuilder;
	   try {
		   docBuilder = dbFactory.newDocumentBuilder();

		   Document XmlResult = docBuilder.newDocument();

		   Element docEleResult = XmlResult.createElement(WoD_Constants.TAG_MESSAGE);
		   docEleResult.setAttribute(WoD_Constants.ATR_TYPE, WoD_Constants.MSG_TYPE_UPDATEGAME_REQUEST);

		   Element Result = XmlResult.createElement(WoD_Constants.TAG_PARTIDA);
		   Result.setAttribute(WoD_Constants.ATR_GAMEID, wipa.getGameID());
		   Result.setAttribute(WoD_Constants.ATR_GAMETITLE, wipa.getGameTitle());
		   Result.setAttribute(WoD_Constants.ATR_SERVERIP, wipa.getServerIP());
		   Result.setAttribute(WoD_Constants.ATR_SERVERPORT, wipa.getServerPort());
		   Result.setAttribute(WoD_Constants.ATR_SERVERSTATUS, wipa.getServerStatus());
		   Result.setAttribute(WoD_Constants.ATR_SERVERNAME, wipa.getServerName());
		   
		   Element Jugadores = XmlResult.createElement(WoD_Constants.TAG_JUGADORES);
		   
		   for (String jugador : wipa.getJugadores()) 
		   {
			   Element Jugador = XmlResult.createElement(WoD_Constants.TAG_JUGADOR);
			   Jugador.setAttribute(WoD_Constants.ATR_PLAYERNAME, jugador);
			   Jugadores.appendChild(Jugador);
		   }
		  
		   Result.appendChild(Jugadores);		   
		   docEleResult.appendChild(Result);		   
		   XmlResult.appendChild(docEleResult);
		   return XmlResult;
	   } catch (ParserConfigurationException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
	   }
	   return null;
   }
   
   public static Document getPartidasMsg()
   {
	   DocumentBuilderFactory dbFactory = DocumentBuilderFactoryImpl.newInstance();
	   DocumentBuilder docBuilder;
	   try {
		   docBuilder = dbFactory.newDocumentBuilder();

		   Document XmlResult = docBuilder.newDocument();

		   Element docEleResult = XmlResult.createElement(WoD_Constants.TAG_MESSAGE);		   
		   docEleResult.setAttribute(WoD_Constants.ATR_TYPE, WoD_Constants.MSG_TYPE_GETPARTIDAS_REQUEST);		   

		  
		   XmlResult.appendChild(docEleResult);

		   System.out.println(WoD_XmlParsing_Client.ParseXmlDocumentToString(XmlResult));
		   
		   return XmlResult;
	   } catch (ParserConfigurationException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
	   } catch (TransformerException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   return null;
   }

   public static Document getStatsMsg(String GameID, String Winner, ArrayList<WoD_InfoJugadoresPartida> Players)
   {
	   DocumentBuilderFactory dbFactory = DocumentBuilderFactoryImpl.newInstance();
	   DocumentBuilder docBuilder;
	   try {
		   docBuilder = dbFactory.newDocumentBuilder();

		   Document XmlResult = docBuilder.newDocument();

		   Element docEleResult = XmlResult.createElement(WoD_Constants.TAG_MESSAGE);		   
		   docEleResult.setAttribute(WoD_Constants.ATR_TYPE, WoD_Constants.MSG_TYPE_STATSGAME_REQUEST);
		   
		   Element Partida = XmlResult.createElement(WoD_Constants.TAG_PARTIDA);
		   Partida.setAttribute(WoD_Constants.ATR_GAMEID, GameID);
		   Partida.setAttribute(WoD_Constants.ATR_GANADOR, Winner);
		   
		   Element Jugadores = XmlResult.createElement(WoD_Constants.TAG_JUGADORES);
		   
		   for (WoD_InfoJugadoresPartida woD_InfoJugadoresPartida : Players) {
			   Element Jugador = XmlResult.createElement(WoD_Constants.TAG_JUGADOR);
			   
			   Jugador.setAttribute(WoD_Constants.ATR_PLAYERNAME, woD_InfoJugadoresPartida.getPlayerName());
			   Jugador.setAttribute(WoD_Constants.ATR_ROL, woD_InfoJugadoresPartida.getRole());
			   
			   Jugadores.appendChild(Jugador);
		   }
		   Partida.appendChild(Jugadores);
		   docEleResult.appendChild(Partida);
		   XmlResult.appendChild(docEleResult);

		   System.out.println(WoD_XmlParsing_Client.ParseXmlDocumentToString(XmlResult));
		   
		   return XmlResult;
	   } catch (ParserConfigurationException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
	   } catch (TransformerException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   return null;
   }

   public static Document getPlayersNumberMsg(String GameID)
   {
	   DocumentBuilderFactory dbFactory = DocumentBuilderFactoryImpl.newInstance();
	   DocumentBuilder docBuilder;
	   try {
		   docBuilder = dbFactory.newDocumentBuilder();

		   Document XmlResult = docBuilder.newDocument();

		   Element docEleResult = XmlResult.createElement(WoD_Constants.TAG_MESSAGE);

		   docEleResult.setAttribute(WoD_Constants.ATR_TYPE, WoD_Constants.MSG_TYPE_PLAYERSNUMBER_REQUEST);

		   Element Result = XmlResult.createElement(WoD_Constants.TAG_PARTIDA);
		   Result.setAttribute(WoD_Constants.ATR_GAMEID, GameID);

		   docEleResult.appendChild(Result);		   
		   XmlResult.appendChild(docEleResult);

		   return XmlResult;
	   } catch (ParserConfigurationException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
	   }
	   return null;
	   
   }
   
   public static Document getCheckConnectionMsg()
   {
	   DocumentBuilderFactory dbFactory = DocumentBuilderFactoryImpl.newInstance();
	   DocumentBuilder docBuilder;
	   try {
		   docBuilder = dbFactory.newDocumentBuilder();

		   Document XmlResult = docBuilder.newDocument();

		   Element docEleResult = XmlResult.createElement(WoD_Constants.TAG_MESSAGE);

		   docEleResult.setAttribute(WoD_Constants.ATR_TYPE, WoD_Constants.MSG_TYPE_CHECKCONNECTION_REQUEST);

		   XmlResult.appendChild(docEleResult);

		   return XmlResult;
	   } catch (ParserConfigurationException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
	   }
	   return null;
	   
   }
}
